package dao;
import java.util.List;

import org.hibernate.classic.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;
import entity.Compra;
import entity.ContasPagar;

public class ContasPagarDaoImplementation implements ContasPagarDao {

	public void save(ContasPagar contasPagar) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(contasPagar);
		transaction.commit();
		session.close();
	}

	public void update(ContasPagar contasPagar) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(contasPagar);
		transaction.commit();
		session.close();
	}

	public void delete(ContasPagar contasPagar) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(contasPagar);
		transaction.commit();
		session.close();
	}

	public void consult(ContasPagar contasPagar) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		contasPagar = (ContasPagar) session.load(ContasPagar.class, new String("ContasPagar"));
	}

	public ContasPagar getContasPagar(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (ContasPagar) session.load(ContasPagar.class, id);
	}

	@Override
	public List<ContasPagar> list() {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "SELECT new ContasPagar( c.id, c.valorPago, c.juros, c.dataPagamento, c.itemParcelaCompra) FROM ContasPagar c";
        List<ContasPagar> lista = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return lista;
	}
	
	

}
